var RegNumber	= url.templateArgs["RegNumber"];
var RegGroup 	= url.templateArgs["RegGroup"];
var RegTitle	= url.templateArgs["RegTitle"];
var RegNote 	= url.templateArgs["RegNote"];
var Initiator 	= url.templateArgs["Initiator"];
var Mode 		= url.templateArgs["Mode"];
var Documents	= url.templateArgs["Attachments"];

var site = "sakym-personalo-klausimais-registravimas";
var datalistRef = "b7bbef3c-5803-45e3-b009-7e79207bc062";
var referenceType = "node";
var data = new Date();

function returnNode(node){
    var reference = ["workspace", "SpacesStore", node];
    var foundNode = search.findNode(referenceType, reference);
    return foundNode;
}

var personaloPranList = companyhome.childByNamePath("Sites/"+site+"/dataLists/"+datalistRef); 
var DocArray = Documents.split("|");

var iras = personaloPranList.createNode(null, "rcc:personaloPranList");
iras.properties["rcc:perPranRegGroup"] = RegGroup;
iras.properties["rcc:perPranRegNumber"] = RegNumber;
iras.properties["rcc:perPranRegData"] = data;
iras.properties["rcc:perPranRegTitle"] = RegTitle;
iras.properties["rcc:perPranRegNote"] = RegNote;
iras.save();
iras.createAssociation(person,"rcc:perPranRegistrator");

var iniciator= people.getPerson(Initiator);
if (iniciator != null && iniciator != "" && iniciator != "undefined") {
    iras.createAssociation(iniciator,"rcc:iniciator");
}

if (Mode == "multiple") {
	for (var i = 0; i < DocArray.length; i++){
		var reference = ["workspace", "SpacesStore", DocArray[i]];
		var foundNode = search.findNode(referenceType, reference);
		if (foundNode.properties["rcc:dokTip"]  == 100) {
			iras.createAssociation(returnNode(DocArray[i]),"rcc:attachments");
		}
	}
} else {
	for (var i=0;i<DocArray.length;i++){
	   iras.createAssociation(returnNode(DocArray[i]),"rcc:attachments");
	}
}

if (iras.nodeRef != null) {
    status.code=200;
    model.message= "success";
} else {
    status.redirect=true;
    status.code=404;
    model.message= "error";
}